import {defineStore} from 'pinia';

const statusOption = [
    {label: "正常", value: "0"},
    {label: "停用", value: "1"}
]

const API = {
    LIST: "/user/page",
    USERSAVE: "/role/user/assign",
}

const COLUMNS = [
    {field: "id", title: "用户编号", align: "center", width: "100px"},
    {field: "userName", title: "账号", align: "center", width: "150px"},
    {field: "nickName", title: "用户昵称", align: "center", width: "150px"},
    {field: "sexName", title: "性别", align: "center", width: "100px"},
    {field: "phone", title: "手机号码", align: "center", width: "150px"},
    {field: "status", title: "状态", align: "center", width: "150px"},
    {field: "createTime", title: "创建时间", align: "center"},
]

const SEARCH_FORM = [
    {field: "userName", title: "账号", placeholder: "请输入用户名称"},
    {field: "status", title: "状态", options: statusOption},
]

const OPTIONS = [
    {emit: "assign", title: "分配用户", check: (row) => { return ! row.active }},
    {emit: "cancel", title: "取消分配", check: (row) => { return row.active }},
]

const store = {
    id: "assignUser",
    state(){
        return {
            table: null,
            pageNo: 1,
            pageSize: 10,
            searchForm: {},
            userList: []
        }
    },

    getters: {

    },

    actions: {
        init(table) {
            this.table = table;
            this.table.setColumns(COLUMNS);
            this.table.setSearchForm(SEARCH_FORM);
            this.table.setOptions(OPTIONS);
        },

        refresh() {
            this.getList(1, 10)
        },

        /**
         * 列表数据
         * @param pageNo
         * @param pageSize
         * @param condition
         * @returns {Promise<void>}
         */
        async getList(pageNo, pageSize, condition) {
            let params = {};
            params.pageNo = pageNo ? pageNo : 1;
            params.pageSize = pageSize ? pageSize : 10;
            for (let key in condition) {
                if(! Helper.empty(condition[key])) {
                    params[key] = condition[key];
                }
            }

            Msg.loading("加载中...")
            let {data} = await Http.get(API.LIST, params);
            Msg.loading(false)

            this.pageNo = data.pageNo
            this.pageSize = data.pageSize
            let dataList = []
            if(! Helper.emptyArray(data.dataList)) {
                dataList = data.dataList
                dataList.forEach(i => {
                    i.active = false
                    let userList = this.userList
                    let result = userList.find(child => child == i.id)
                    if (result) {
                        i.active = true
                    }
                })
            }
            this.table.setData(dataList);
            this.table.setPagination(data.pageNo, data.pageSize, data.total);
        },

        /**
         * 批量分配
         * @param id
         * @param userList
         * @returns {Promise<undefined>}
         */
        async userSave(id, userList) {
            let params = {}
            params.id = id
            params.userList = userList

            Msg.loading("正在提交...");
            let {data} = await Http.post(API.USERSAVE, params);
            Msg.loading(false);

            if(data) {
                Msg.success("保存成功");
                this.userList = userList
                this.refresh();
            } else {
                Msg.error("保存失败, 请重新尝试")
            }
            return data
        },
    },
}

export default defineStore(store)